package com.hzsai.dynamicdatasource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * hzsai 2019/8/22
 * usage:
 **/
public class DataSourceContextHolder {

    // 日志打印
    private static final Logger logger = LoggerFactory.getLogger(DataSourceContextHolder.class);

    // 默认数据源
    public static final String defaultDS = "THIRD";

    // String类的ThreadLocal，保证名字是属于线程独有的
    private static final ThreadLocal<DataSourceKey> contextHolder = new ThreadLocal<>();

    public static void setDB(DataSourceKey DSName) {
        contextHolder.set(DSName);
    }

    public static DataSourceKey getDB() {
        return contextHolder.get();
    }

    // 移除当前线程数据源
    public static void clear() {
        contextHolder.remove();
    }
}
